<?php
namespace Home\Controller;

use Think\Controller;
use Think\Page;

class BeautyParlorController extends BaseController {
	/**
	 * 加载美容院
	 */
	public function ajaxLoadBeauty() {
		$province = I('get.province');
		$city = I('get.city');
	
		$Model = D ( 'BeautyParlor' );
		$list = $Model->loadBeautyByLocation($province, $city);
		if ($list) {
			foreach ($list as &$value) {
				$total = $Model->totalBeautyUser($value ['id']);
				$value ['user_total'] = count($total);
			}
			$list = $this->formatAddressDistance($list);
		}
	
		$this->ajaxReturn(array('status' => 1, 'data' => $list));
	}
	
	/**
	 * 美容院页面初始化
	 */
	public function index(){
		$this->assign('select', 'BeautyParlor');
		
		$longitude = I('get.longitude');
		$latitude = I('get.latitude');
		$address = I('get.address');
		
		
		$this->assign('longitude', $longitude);
		$this->assign('latitude', $latitude);
		$this->assign('address', $address);
		
		$this->display();
	}
	
	/**
	 * 美容院的详细信息
	 */
	public function detail() {
		$id = I('get.id');
	
		$BeautyParlorModel = D('BeautyParlor');
		$beautyParlorInfo = $BeautyParlorModel->getBeautyInfo($id);
	
		$PhotoModel = D('Photo');
		$PhotoModel->type = 'cover';
		$covers = $PhotoModel->getTargetPhotos($id);
	
		$this->assign('covers', $covers);
		$ServiceItemModel = D('ServiceItem');
		$this->assign('serviceList', $ServiceItemModel->getServiceList());
	
// 		$lastComment = M('order')->field(array('mc.id'))->where(array('beauty_id' => $id))
// 		->join('o left join mdd_comments as mc on o.beautician_id=mc.to_uid')
// 		->order('mc.createtime desc')
// 		->limit(1)
// 		->find();
	
// 		if ($lastComment) {
// 			$commentId = $lastComment ['id'];
// 			$OrderCommentModel = D('OrderComment');
// 			$comment = $OrderCommentModel->getCommentInfo($commentId);
	
// 			$UserModel = D('User');
// 			$comment ['user_info'] = $UserModel->getUserById($comment ['uid']);
// 			$comment ['start'] = floor($comment ['avg_score']);
	
// 			$commentPhoto = $OrderCommentModel->getCommentPhoto($comment ['id']);
// 			$comment ['photos'] = $commentPhoto;
	
// 			$lastComment ['start'] = floor($lastComment ['avg_score']);
// 			$this->assign('lastComment', $comment);
// 		}
	
		$Employees = M('beauty_parlor_employe')->field('beautician_id')->where(array('bp_id' => $id, 'status' => 1))->select();
		if ($Employees) {
			$beauticianList = array();
			$BeauticianModel = D('Beautician');
			foreach ($Employees as $key => $value) {
				$beauticianInfo = $BeauticianModel->getBeauticianInfo($value ['beautician_id']);
				if ($beauticianInfo ['professional_score'] || $beauticianInfo ['punctuality_score'] || $beauticianInfo ['communication_score']) {
					$beauticianInfo ['start'] = floor(($beauticianInfo ['professional_score'] + $beauticianInfo ['punctuality_score'] + $beauticianInfo ['communication_score'])/3/$beauticianInfo ['comment_total']);
				} else {
					$beauticianInfo ['start'] = 0;
				}
				
				$beauticianList [] = $beauticianInfo;
			}
			
			$this->assign('beauticianList', $beauticianList);
		}
	
		
		$userTotal = D('Order')->field('user_id')->where(array('beauty_id' => $id))->group('user_id')->select();
		$this->assign('user_total', count($userTotal));
		
		if ($beautyParlorInfo ['professional_score'] || $beautyParlorInfo ['punctuality_score'] || $beautyParlorInfo ['communication_score']) {
			$beautyParlorInfo ['start'] = floor(($beautyParlorInfo ['professional_score'] + $beautyParlorInfo ['punctuality_score'] + $beautyParlorInfo ['communication_score'])/3/$beautyParlorInfo ['comment_total']);
		} else {
			$beautyParlorInfo ['start'] = 0;
		}
		
		$beautyParlorInfo ['environment'] = htmlspecialchars_decode($beautyParlorInfo ['environment']);
		$this->assign('item', $beautyParlorInfo);
	
// 		dump($beautyParlorInfo);die;
// 		$zizhi = M('beauty_parlor_zizhi')->where(array('bp_id' => $id))->find();
// 		$this->assign('zizhi', $zizhi);
		$this->display();
	}
	
	/**
	 * 查询用户所在位置离美容院的距离
	 */
	public function getUserToBeautyDistance() {
		$start = I ('get.start');
		$end = I ('get.end');
		 
		if (!isset($start ['latitude']) || !isset($start ['longitude']) ||
		!isset($start ['latitude']) || !isset($start ['longitude']) ) {
			$this->ajaxReturn(array('status' => 0, 'message' => '获取失败'));
		}
		$d = getDistance($start, $end);
		 
		$distance = $d < 1?round($d * 1000).'m':round($d, 1).'km';
		$this->ajaxReturn(array('status' => 1, 'distance' => $distance));
	}
	
	/**
	 * 预约选择美容院操作的页面初始化
	 */
	public function selectBeautyParlor() {
		$serviceId = I ('get.id');
		$beautyId = I ('get.beauty_id');
		$BeautyParlorModel = D('BeautyParlor');
	
		$list = $BeautyParlorModel->getBeautyParlor();
		if ($list) {
			foreach ($list as &$value) {
				$total = $BeautyParlorModel->totalBeautyUser($value ['id']);
				$value ['user_total'] = count($total);
			}
		}
	
		$this->assign('serviceId', $serviceId);
		$this->assign('beauty_id', $beautyId);
		$this->assign('list', $list);
		$this->display();
	}
	
	/**
	 * 美容院资质
	 */
	public function zizhi() {
		$id = I('get.id');
	
		$BeautyParlorModel = D('BeautyParlor');
		$beautyParlorInfo = $BeautyParlorModel->getBeautyInfo($id);
		$this->assign('item', $beautyParlorInfo);
	
		$zizhi = M('beauty_parlor_zizhi')->where(array('bp_id' => $id))->find();
		$this->assign('zizhi', $zizhi);
		$this->display();
	}
    
}